﻿package plugin.qryl
{
    import cmd.*;
    import com.core.*;
    import com.core.destroy.*;
    import com.greensock.*;
    import com.sh.game.display.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;
    import plugin.component.view.*;
    import pluginfw.nio.*;
    import uiresource.*;
    import view.mornExtends.*;
    import vo.*;

    public class QiRiYouLiUICtrl extends NewQRYLUI
    {
        private var _close:Function;
        private var cfgBox:Object;
        private var cfgItem:Object;
        private var cfgHuodong:Object;
        private var mainArr:Array;
        private var itemArr:Array;
        private var curLoginDay:int = 0;
        private var selectDay:int = 0;
        private var rewardState:int = 0;
        private var qrylInfo:Object;
        private var timeline:TweenMax;
        private var mc:XMLMovieClip;
        private var tweenMax:TweenMax;
        private var mCurModel:RoleShow;
        private var mHorseCfg:Object;
        private var effectSprite:Sprite;

        public function QiRiYouLiUICtrl(param1:Function)
        {
            this.cfgBox = ConfigDictionary.data.box;
            this.cfgItem = ConfigDictionary.data.item;
            this.cfgHuodong = ConfigDictionary.data.huodong;
            this.mHorseCfg = ConfigDictionary.data.wing;
            this.effectSprite = new Sprite();
            this._close = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(QiRiYouLiControl, this);
            ModelProxy.addObserver("qryl", _loc_1);
            this.qrylInfo = ModelProxy.getValue("data", "qrylInfo");
            XMLMcManager.instance.complatefuncs["qryl"] = this.mcCompletefunc;
            return;
        }// end function

        private function mcCompletefunc() : void
        {
            if (this.selectDay == 2)
            {
                return;
            }
            this.mc = XMLMcManager.instance.getPluginMc("qryl", "qryl_" + this.selectDay);
            if (this.mc)
            {
                this.effectSprite.addChild(this.mc);
                this.mc.mouseChildren = false;
                this.mc.mouseEnabled = false;
                this.mc.interval = 1000 / 5;
                if (this.selectDay == 1 || this.selectDay == 3 || this.selectDay == 6)
                {
                    this.mc.x = -51 + 18;
                    this.mc.y = 174 - 24 - 15;
                    itemBottom.visible = true;
                }
                else if (this.selectDay == 5)
                {
                    this.mc.interval = 1000 / 8;
                    this.mc.x = -51 + 40 - 228;
                    this.mc.y = 174 - 25 - 192;
                    itemBottom.visible = false;
                }
                else if (this.selectDay == 4)
                {
                    this.mc.x = -51 + 18;
                    this.mc.y = 174 - 14;
                    itemBottom.visible = true;
                }
                else if (this.selectDay == 7)
                {
                    this.mc.x = -51 + 18 - 113;
                    this.mc.y = 174 - 24 - 15 - 17 - 70;
                    itemBottom.visible = true;
                }
            }
            return;
        }// end function

        public function openRefresh() : void
        {
            this.qrylInfo = ModelProxy.getValue("data", "qrylInfo");
            this.init(0, this.qrylInfo);
            return;
        }// end function

        public function init(param1:int, param2:Object) : void
        {
            var _loc_4:Object = null;
            var _loc_5:Array = null;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_9:Boolean = true;
            selectImg.visible = true;
            titleSelectImg.visible = _loc_9;
            this.curLoginDay = param2.loginDayCount;
            if (param1 == 0)
            {
                this.selectDay = param2.loginDayCount;
            }
            this.rewardState = param2.rewardState;
            if (this.rewardState == 254)
            {
                ModelProxy.change("mainui", "removeBtn", "qryl");
            }
            this.mainArr = [];
            this.itemArr = [];
            var _loc_3:int = 0;
            while (_loc_3 < 7)
            {
                
                _loc_4 = this.cfgBox[int(this.cfgHuodong[1].itemid) + _loc_3];
                _loc_5 = (_loc_4.items as String).split(";");
                this.itemArr[_loc_3] = [];
                this.mainArr[_loc_3] = [];
                _loc_6 = 0;
                while (_loc_6 < _loc_5.length)
                {
                    
                    _loc_7 = _loc_5[_loc_6].split("-");
                    if (_loc_7.length >= 5 && _loc_7[4] > 0 && _loc_7[4] != UserData.getUserData("career"))
                    {
                    }
                    else if (_loc_7.length >= 4 && _loc_7[3] != 2 && _loc_7[3] != UserData.getUserData("sex"))
                    {
                    }
                    else
                    {
                        this.itemArr[_loc_3].push(_loc_5[_loc_6]);
                        this.mainArr[_loc_3] = this.itemArr[_loc_3][0];
                    }
                    _loc_6++;
                }
                this.showLingQuImg((_loc_3 + 1));
                _loc_3++;
            }
            list1.array = this.mainArr;
            list1.renderHandler = new Handler(this.mainListRender);
            this.initEvent();
            if (param1 == 1)
            {
                _loc_8 = this.addDay(this.selectDay);
                this.selectDay = _loc_8;
            }
            else if (this.isGetReward(this.selectDay))
            {
                (this.selectDay + 1);
            }
            if (this.selectDay > 7)
            {
                this.selectDay = 7;
            }
            line.width = 10 + (this.selectDay - 1) * 107;
            list1.selectedIndex = this.selectDay - 1;
            this.showInfo(this.selectDay);
            return;
        }// end function

        private function addDay(param1:int) : int
        {
            var _loc_2:* = param1 + 1;
            while (this.isGetReward(_loc_2))
            {
                
                _loc_2 = _loc_2 + 1;
            }
            if (_loc_2 >= 7)
            {
                _loc_2 = 7;
            }
            return _loc_2;
        }// end function

        private function mainListRender(param1:Component, param2:int) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Object = null;
            var _loc_5:FlashItem = null;
            var _loc_6:UrlImage = null;
            var _loc_7:UrlImage = null;
            if (param2 < list1.length)
            {
                _loc_3 = (list1.array[param2] as String).split("-");
                _loc_4 = ItemObject.create(this.cfgItem[_loc_3[0]]);
                _loc_5 = param1.getChildByName("fi") as FlashItem;
                _loc_5.data = _loc_4;
                _loc_6 = param1.getChildByName("nameImg") as UrlImage;
                _loc_6.url = "cs2_qryl/day" + String((param2 + 1)) + ".png";
                _loc_7 = param1.getChildByName("day_faguang1") as UrlImage;
                _loc_7.url = "cs2_qryl/title_fa_" + (param2 + 1) + ".png";
            }
            return;
        }// end function

        private function initEvent() : void
        {
            closeBtn.addEventListener(MouseEvent.CLICK, this.closeHandler);
            sureBtn.addEventListener(MouseEvent.CLICK, this.lingquHandler);
            list1.selectHandler = new Handler(this.listHandler);
            return;
        }// end function

        private function selectHandler(event:MouseEvent) : void
        {
            var _loc_2:* = int(event.currentTarget.name);
            this.selectDay = _loc_2;
            this.showInfo(_loc_2);
            return;
        }// end function

        private function listHandler(param1:int) : void
        {
            this.selectDay = list1.selectedIndex + 1;
            this.showInfo(this.selectDay);
            return;
        }// end function

        private function clearTween() : void
        {
            if (this.timeline)
            {
                this.timeline.kill();
            }
            sureBtn.filters = [];
            if (sureBtn.disabled)
            {
                ObjectUtils.gray(sureBtn);
            }
            if (this.tweenMax)
            {
                this.tweenMax.kill();
            }
            selectImg.alpha = 1;
            return;
        }// end function

        private function showInfo(param1:int) : void
        {
            this.clearTween();
            if (this.curLoginDay < param1)
            {
                sureBtn.disabled = true;
                sureBtn.label = "领取奖励";
                dayCountLabel.visible = true;
                dayCountLabel.text = "已登录天数：(" + String(this.curLoginDay) + "/" + String(param1) + ")";
            }
            else
            {
                dayCountLabel.visible = false;
                if (this.isGetReward(param1))
                {
                    sureBtn.disabled = true;
                    sureBtn.label = "已领取";
                }
                else
                {
                    sureBtn.disabled = false;
                    sureBtn.label = "领取奖励";
                    this.timeline = TweenMax.to(sureBtn, 1, {glowFilter:{color:16763904, alpha:1, blurX:15, blurY:15, strength:2}, repeat:10000, yoyo:true});
                }
            }
            this.showLingQuImg(param1);
            selectImg.x = 45 + (param1 - 1) * 107 - 1;
            this.tweenMax = TweenMax.to(selectImg, 1, {alpha:0.2, repeat:10000, yoyo:true});
            if (param1 == 1)
            {
                titleSelectImg.x = 37;
            }
            else
            {
                titleSelectImg.x = 143 + (param1 - 2) * 108;
            }
            day_normal1.url = "cs2_qryl/title_normal" + param1 + ".png";
            this.showZhanShiEffect(param1);
            this.showRewards(param1);
            return;
        }// end function

        private function showZhanShiEffect(param1:int) : void
        {
            if (this.effectSprite.parent == null)
            {
                this.addChild(this.effectSprite);
                this.effectSprite.mouseChildren = false;
                this.effectSprite.mouseEnabled = false;
            }
            DestroyUtil.removeChildren(this.effectSprite);
            if (param1 == 2)
            {
                itemBottom.visible = false;
                if (!this.mCurModel)
                {
                    this.mCurModel = new RoleShow("4107", 4, null, this.mHorseCfg[13], null);
                }
                this.mCurModel.x = -51 + 343;
                this.mCurModel.y = 174 + 377;
                this.mCurModel.mouseEnabled = false;
                this.effectSprite.addChild(this.mCurModel);
            }
            else
            {
                this.mc = XMLMcManager.instance.getPluginMc("qryl", "qryl_" + param1);
                if (this.mc)
                {
                    this.effectSprite.addChild(this.mc);
                    this.mc.mouseChildren = false;
                    this.mc.mouseEnabled = false;
                    this.mc.interval = 1000 / 5;
                    if (param1 == 1 || param1 == 3 || param1 == 6)
                    {
                        this.mc.x = -51 + 18;
                        this.mc.y = 174 - 24 - 15;
                        itemBottom.visible = true;
                    }
                    else if (param1 == 5)
                    {
                        this.mc.interval = 1000 / 8;
                        this.mc.x = -51 + 40 - 228;
                        this.mc.y = 174 - 25 - 192;
                        itemBottom.visible = false;
                    }
                    else if (param1 == 4)
                    {
                        this.mc.x = -51 + 18;
                        this.mc.y = 174 - 14;
                        itemBottom.visible = true;
                    }
                    else if (param1 == 7)
                    {
                        this.mc.x = -51 + 18 - 113;
                        this.mc.y = 174 - 24 - 15 - 17 - 70;
                        itemBottom.visible = true;
                    }
                }
            }
            return;
        }// end function

        private function clearEffect() : void
        {
            if (this.mCurModel)
            {
                this.mCurModel.destroy();
                this.mCurModel = null;
            }
            if (this.mc)
            {
                if (this.mc.parent)
                {
                    this.mc.parent.removeChild(this.mc);
                }
            }
            return;
        }// end function

        private function showRewards(param1:int) : void
        {
            var _loc_2:* = this.itemArr[(param1 - 1)];
            itemList2.array = _loc_2;
            itemList2.renderHandler = new Handler(this.listRender2);
            return;
        }// end function

        private function listRender2(param1:Component, param2:int) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Object = null;
            if (param2 < itemList2.length)
            {
                _loc_3 = (itemList2.array[param2] as String).split("-");
                _loc_4 = ItemObject.create(this.cfgItem[_loc_3[0]]);
                _loc_4.count = _loc_3[1];
                (param1 as FlashItem).flagshow = true;
                (param1 as FlashItem).effshow = true;
                (param1 as FlashItem).data = _loc_4;
            }
            return;
        }// end function

        private function isGetReward(param1:int) : Boolean
        {
            var _loc_3:int = 0;
            var _loc_2:* = param1;
            _loc_3 = this.rewardState >> _loc_2;
            var _loc_4:* = _loc_3 % 2 != 0;
            return _loc_3 % 2 != 0;
        }// end function

        private function showLingQuImg(param1:int) : void
        {
            if (param1 < 1)
            {
                return;
            }
            if (this.isGetReward(param1))
            {
                this["lingqu" + param1].visible = true;
            }
            else
            {
                this["lingqu" + param1].visible = false;
            }
            return;
        }// end function

        private function lingquHandler(event:MouseEvent) : void
        {
            var _loc_2:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Point = null;
            if (_loc_2)
            {
                while (_loc_2.length > 0)
                {
                    
                    _loc_2.pop();
                }
                _loc_2 = null;
            }
            _loc_2 = [];
            var _loc_3:int = 0;
            while (_loc_3 < itemList2.array.length)
            {
                
                _loc_5 = 349 + _loc_3 * (70 + 7);
                _loc_6 = 376;
                _loc_7 = localToGlobal(new Point(_loc_5, _loc_6));
                _loc_2[_loc_3] = {x:_loc_7.x, y:_loc_7.y};
                _loc_3++;
            }
            ModelProxy.change("data", "qrylItemPoint", _loc_2);
            var _loc_4:* = new Output();
            new Output().writeVarInt(this.selectDay, false);
            PluginManager.instance.sendToBufferServer("activity", CMDActivity.GetQRYL, _loc_4);
            return;
        }// end function

        private function closeHandler(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        private function close() : void
        {
            if (this._close != null)
            {
                this._close();
            }
            this.clearEffect();
            list1.selectedIndex = -1;
            var _loc_1:Boolean = false;
            selectImg.visible = false;
            titleSelectImg.visible = _loc_1;
            this.clearTween();
            return;
        }// end function

    }
}
